package com.weighing.web.controller.server;

import com.weighing.common.core.controller.BaseController;
import com.weighing.common.core.domain.AjaxResult;
import com.weighing.system.service.server.WeighingMasterService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/weighing/weighingMaster")
public class WeighingMasterController extends BaseController {

    @Resource
    private WeighingMasterService weighingMasterService;

    /**
     * 校验重量
     * @param billNo 工单号
     * @param subMaterialId 物料ID
     * @param weight 重量
     * @return boolean
     */
    @GetMapping("/checkWeighing")
    public AjaxResult checkWeighing(@RequestParam(value = "billNo", required = false) String billNo, @RequestParam(value = "subMaterialId") String subMaterialId, @RequestParam("weight") Double weight){
        return success(weighingMasterService.checkWeighing(billNo, subMaterialId, weight));
    }

    /**
     * 密码匹配
     * @param userId 用户ID
     * @param rawPassword 原始密码
     * @return  boolean
     */
    @GetMapping("/matchesPassword/{userId}/{rawPassword}")
    public AjaxResult matchesPassword(@PathVariable("userId") String userId, @PathVariable("rawPassword") String rawPassword) {
        return success(weighingMasterService.matchesPassword(userId, rawPassword));
    }

    /**
     * 添加称重明细
     *
     * @param billNo     工单号
     * @param materialId 物料id
     * @param weight     重量
     * @return 添加结果
     */
    @GetMapping("/addWeighingDetail")
    public synchronized AjaxResult addWeighingDetail(@RequestParam String billNo, @RequestParam String materialId, @RequestParam Double weight,
                                                     @RequestParam(value = "authorizerPersonId", required = false) String authorizerPersonId,
                                                     @RequestParam(value = "authorizerPersonName", required = false) String authorizerPersonName) {
        weighingMasterService.addWeighingDetail(billNo, materialId, weight, authorizerPersonId, authorizerPersonName);
        return AjaxResult.success();
    }

    /**
     * 根据单据号获取称重单信息
     *
     * @param billNo 单据号
     * @return 称重单信息
     */
    @GetMapping("/getWeighingMasterByBillNo/{billNo}")
    public AjaxResult getWeighingMasterByBillNo(@PathVariable String billNo) {
        return success(weighingMasterService.getWeighingMasterByBillNo(billNo));
    }

    /**
     * 称重单完成
     *
     * @param billNo 单据编号
     * @return 结果
     */
    @GetMapping("/weighingMasterFinished/{billNo}")
    public AjaxResult weighingMasterFinished(@PathVariable String billNo) {
        weighingMasterService.weighingMasterFinished(billNo);
        return success();
    }

    /**
     * 根据单据号获取下料单信息
     *
     * @param billNo 单据号
     * @return 下料单信息
     */
    @GetMapping("/getWeighingMaster006ByBillNo/{billNo}")
    public AjaxResult getWeighingMaster006ByBillNo(@PathVariable String billNo) {
        return success(weighingMasterService.getWeighingMaster006ByBillNo(billNo));
    }

    /**
     * 扫码下料
     * @param billNo 单据编号
     * @param rowCode 行码
     * @return success
     */
    @GetMapping("/scanWeighingDetail/{billNo}/{rowCode}")
    public AjaxResult scanWeighingDetail(@PathVariable String billNo, @PathVariable Integer rowCode) {
        weighingMasterService.scanWeighingDetail(billNo, rowCode);
        return success();
    }

}
